function outcell = tex2string(incell,settings)
% Function to convert latex string to form that can be printed by Matlab to
% a file or used for titles and subtitles.  
%
% Right now, this function does the following modifications:
%   1) Adds a backslash before a backslash to retain it
%   2) Adds a backslash before a carot to retain it
%   3) Adds a backslash before an underscore to retain it
% ===========================================================================
% [Input]
% incell       : the latex names you want to convert
% settings.type: the type of file.  Currently, the following options are
%               available
%               1) default: retain all backslashes; carots; underscores
%               2) file: retain only carots; underscores
%               3) print: retains only backslashes
% settings.mode: whether to put in "math" mode or "text" mode.
%
% [Output]
% outcell : cell of string names that can be printed to file
% ============================================================================
%
% Edited 8/11/2014 by TM
%
% To Do: Fix the carots and underscores to remove the potential curly
% brackets following them.

% Note that to keep using slashes in regexprep, you must preempt them with
% a slash.

%% Preliminaries
% Set type if not there
if nargin<2
    settings.type     = 'default';
    settings.mode     = 'text';  
end

if nargin==2
    if ~isfield(settings,'type')
        settings.type = 'default';
    end
    
    if ~isfield(settings,'mode')
        settings.mode = 'text';
    end
end

% Check type to make sure we can use it.
settings.type = lower(settings.type);
if ~ismember(settings.type,{'default' 'file' 'print'});
    error('Wrong filetype')
end

settings.mode = lower(settings.mode);
if ~ismember(settings.mode,{'text' 'math'})
    error('Wrong mode')
end


%% Create Flags
% We'll make sure to output a character if that's what it is. 
if ischar(incell)
    incell = {incell};
    flag.str = 1;
else
    flag.str = 0;
end

% Add the dollar signs around the text?
if strcmpi(settings.mode,'math')
    flag.math = 1;
else
    flag.math = 0;
end

% Backslashes?
if ismember(settings.type,{'default' 'print'})
    flag.backslash = 1;
else
    flag.backslash = 0;
end


% Carots?
if ismember(settings.type,{'default' 'file'})
    flag.carot = 1;
else
    flag.carot = 0;
end

% Underscores?
if ismember(settings.type,{'default' 'file'})
    flag.underscore = 1;
else
    flag.underscore = 0;
end


%% Convert String
nCells = length(incell);
outcell = cell(size(incell));

for n = 1:nCells
    % Initialize
    outcell{n} = incell{n};
    
    % Replace
    if flag.backslash;  outcell{n} = regexprep(outcell{n},'\','\\\');    end;
    if flag.carot;      outcell{n} = regexprep(outcell{n},'^','\\^');     end;
    if flag.underscore; outcell{n} = regexprep(outcell{n},'_','\\_');     end;

end

if flag.math
    outcell = cellfun(@(x) ['$',x,'$'],outcell,'UniformOutput',false);
end

%% Output outcell
% Remember to convert back to a string if necessary
if flag.str
    outcell = char(outcell);
end


end